package com.gaogzhen.algorithms4.foundation;

import edu.princeton.cs.algs4.Bag;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;

public class TestBag {
    public static void main(String[] args) {
        Bag<Double> numbers = new Bag<>();

        while (!StdIn.isEmpty())
            numbers.add(StdIn.readDouble());

        int size = numbers.size();

        double sum = 0;
        for (double x: numbers)
            sum += x;

        double avg = sum / size;

        sum = 0;
        for (double x: numbers)
            sum += (x - avg) * (x - avg);

        double std = Math.sqrt(sum / (size - 1));
        StdOut.printf("avg:%.2f\n", avg);
        StdOut.printf("Std dev:%.2f\n", std);
    }
}
